//
//  LookView.m
//  Kugou-OC
//
//  Created by BH on 16/1/22.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "LookView.h"
#import "GlobalDefine.h"
#import <UIView+SDAutoLayout.h>
#import "LookViewCollectionCell.h"
#import "LookViewCollectionReusableView.h"

@interface LookView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
{
    NSArray *itemTitle;
    NSArray *itemImg;
}

@property(nonatomic,strong)UICollectionView *collectionV;

@end

@implementation LookView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        itemTitle = @[@"同城",@"广场",@"好声音",@"女神",@"男神",@"新秀",@"酷拍",@"发现",@"我的"];
        itemImg = @[@"home_cell_rectangle_image1.jpg",@"home_cell_rectangle_image2.jpg",@"home_cell_rectangle_image3.jpg",@"home_cell_rectangle_image4.jpg",@"home_cell_rectangle_image5.jpg",@"home_cell_rectangle_image6.jpg",@"home_cell_rectangle_image7.jpg",@"home_cell_rectangle_image8.jpg",@"home_cell_rectangle_image9.jpg"];
        
        //添加子视图
        [self addAlphaBgViews];
    }
    
    return self;
}

-(void)addAlphaBgViews{
    UIView *bgView1 = [[UIView alloc] init];
    bgView1.layer.masksToBounds = NO;
    bgView1.layer.cornerRadius = 3;
    bgView1.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView2 = [[UIView alloc] init];
    bgView2.layer.masksToBounds = NO;
    bgView2.layer.cornerRadius = 3;
    bgView2.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView3 = [[UIView alloc] init];
    bgView3.layer.masksToBounds = NO;
    bgView3.layer.cornerRadius = 3;
    bgView3.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    [self sd_addSubviews:@[bgView1,bgView2,bgView3]];
    
    bgView1.sd_layout.leftSpaceToView(self,10).topSpaceToView(self,10).widthRatioToView(self,0.63);
    bgView2.sd_layout.leftSpaceToView(bgView1,5).topEqualToView(bgView1).rightSpaceToView(self,10).heightIs(bgView1.height);
    bgView3.sd_layout.leftEqualToView(bgView1).topSpaceToView(bgView1,10).rightEqualToView(bgView2);
    
    [self addBgView1Subviews:bgView1];
    [self addBgView2Subviews:bgView2];
    [self addBgView3Subviews:bgView3];
}

-(void)addBgView1Subviews:(UIView *)view{
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"MV";
    label1.font = TITLE_ONE_FONT;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"内地|港台|韩国|欧美";
    label2.font = TITLE_FOUR_FONT;
    label2.textColor = TITLE_SECOND_TEXT_COLOR;
    
    UIImageView *arrowImg = [[UIImageView alloc] init];
    arrowImg.image = IMAGE(@"home_cell_arrow_left");
    
    [view sd_addSubviews:@[label1,label2,arrowImg]];
    
    label1.sd_layout.leftSpaceToView(view,20).topSpaceToView(view,20).widthIs(60).autoHeightRatio(0);
    label2.sd_layout.leftEqualToView(label1).topSpaceToView(label1,20).autoHeightRatio(0).widthIs(200);
    arrowImg.sd_layout.rightSpaceToView(view,20).centerYEqualToView(view).widthIs(10).heightEqualToWidth();
    [view setupAutoHeightWithBottomView:label2 bottomMargin:20];
}

-(void)addBgView2Subviews:(UIView *)view{
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"酷狗Live";
    label1.font = TITLE_ONE_FONT;
    label1.textAlignment = NSTextAlignmentCenter;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"星乐坊";
    label2.font = TITLE_FOUR_FONT;
    label2.textColor = TITLE_SECOND_TEXT_COLOR;
    label2.textAlignment = NSTextAlignmentCenter;
    
    UIImageView *pointImg = [[UIImageView alloc] init];
    pointImg.image = IMAGE(@"look_red_point");
    
    [view sd_addSubviews:@[label1,label2,pointImg]];
    
    label1.sd_layout.leftSpaceToView(view,0).topSpaceToView(view,20).rightEqualToView(view).autoHeightRatio(0);
    label2.sd_layout.leftEqualToView(label1).topSpaceToView(label1,20).autoHeightRatio(0).rightEqualToView(label1);
    pointImg.sd_layout.rightSpaceToView(view,10).bottomSpaceToView(label1,0).widthIs(10).heightEqualToWidth();
    [view setupAutoHeightWithBottomView:label2 bottomMargin:20];
}

-(void)addBgView3Subviews:(UIView *)view{
    //确定是水平滚动，还是垂直滚动
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setHeaderReferenceSize:CGSizeMake(SCREEN_WIDTH-20, 60)];
    
    self.collectionV=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionV.dataSource=self;
    self.collectionV.delegate=self;
    [self.collectionV setBackgroundColor:[UIColor clearColor]];
    //注册Cell，必须要有
    [self.collectionV registerClass:[LookViewCollectionCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.collectionV registerClass:[LookViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [view addSubview:self.collectionV];
    
    if (itemTitle.count%3==0) {
        CGFloat h = itemTitle.count/3*100+flowLayout.headerReferenceSize.height;
        self.collectionV.sd_layout.leftSpaceToView(view,0).topSpaceToView(view,0).rightSpaceToView(view,0).heightIs(h);
    }else{
        CGFloat h = (itemTitle.count/3+1)*100+flowLayout.headerReferenceSize.height;
        self.collectionV.sd_layout.leftSpaceToView(view,0).topSpaceToView(view,0).rightSpaceToView(view,0).heightIs(h);
    }
    
    [view setupAutoHeightWithBottomView:self.collectionV bottomMargin:10];
    [self setupAutoContentSizeWithBottomView:view bottomMargin:10];
}

#pragma mark - UICollectionView datasource & delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return itemTitle.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"UICollectionViewCell";
    LookViewCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.itemImg.image = IMAGE(itemImg[indexPath.row]);
    cell.itemLab.text = itemTitle[indexPath.row];
    
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    LookViewCollectionReusableView *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    
    return headerV;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH-80)/3, 90);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 20, 0, 20);
}

@end
